//
//  OMCycleView.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/24.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMCycleView.h"
#import "NSTimer+STBlock.h"
@interface OMCycleViewImageData : NSObject
@property (nonatomic, strong) NSString *image;
+ (instancetype)dataWithImage:(NSString *)image;
@end
@implementation OMCycleViewImageData
+ (instancetype)dataWithImage:(NSString *)image
{
    OMCycleViewImageData *data = [[OMCycleViewImageData alloc]init];
    data.image = image;
    return data;
}


@end

#import "NSTimer+STBlock.h"
@interface OMCycleView() <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray<UIImageView*>* imageViews;
@property (nonatomic, assign) BOOL canScroll;

@property (nonatomic, assign) BOOL isDragging;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation OMCycleView {
    BOOL _islocal;
    NSArray *_imageDatas;
}

- (instancetype)init{
    self = [super init];
    [self configure];
    return self;
}

- (void)setImages:(NSArray <NSString *>*)images IsLocal:(BOOL)islocal
{
    _canScroll = images.count>1;
    _islocal = islocal;
    NSMutableArray *imageDatas = [NSMutableArray arrayWithCapacity:images.count];
    !_canScroll?:[imageDatas addObject:[OMCycleViewImageData dataWithImage:images.lastObject]];
    for (NSString *image in images) {
    [imageDatas addObject:[OMCycleViewImageData dataWithImage:image]];
    }
    !_canScroll?:[imageDatas addObject:[OMCycleViewImageData dataWithImage:images.firstObject]];
    _imageDatas = imageDatas;
    [self reload];
}

- (void)reload
{
    [self killTimer];
    for (UIImageView *imageView in self.imageViews) {
        [imageView removeFromSuperview];
    }
    [self.imageViews removeAllObjects];

    NSUInteger index = 0;
    for (OMCycleViewImageData *data in _imageDatas) {
        UIImageView *imageView = [UIImageView new];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        if (_islocal) {
            imageView.image = [UIImage imageNamed:data.image];
        }else {
            [imageView kt_imageWithURLStr:data.image placeHolder:[UIImage imageNamed:@"placehold_banner"] withSize:CGSizeZero];
        }
        [self.scrollView addSubview:imageView];
        [self.imageViews addObject:imageView];
        imageView.frame = CGRectMake(self.width*index, 0, self.width, self.height);
        index++;
    }

    self.scrollView.contentSize = CGSizeMake(self.width*self.imageViews.count, 0);
    if (self.canScroll) {
        [self createTimer];
    }
    [self handleWithScrollView:self.scrollView];
}

- (void)configure
{
    _scrollView = [UIScrollView new];
    _scrollView.pagingEnabled = true;
    _scrollView.delegate = self;
    _scrollView.showsHorizontalScrollIndicator = false;
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    _imageViews = [NSMutableArray arrayWithCapacity:0];

}

- (void)setIsDragging:(BOOL)isDragging
{
    _isDragging = isDragging;
    if (_isDragging) {
        [self killTimer];
    }else {
        if (_canScroll) {
            [self createTimer];
        }
    }
}

- (void)createTimer
{
    @weakify(self);
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 repeats:YES withCallBack:^{
        @strongify(self);
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x+self.width, 0) animated:true];
    }];
    [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)scrollToFirst
{
    [self.scrollView setContentOffset:CGPointMake(self.width, 0)];
}
- (void)scrollToLast
{
    [self.scrollView setContentOffset:CGPointMake((self.imageViews.count-2)*self.width, 0)];
}

- (void)setIndex:(NSUInteger)index
{
    _index = index;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didScrollToIndex:)]) {
        [self.delegate didScrollToIndex:index];
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.isDragging = true;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.isDragging = false;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   [self handleWithScrollView:scrollView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self handleWithScrollView:scrollView];
}

- (void)handleWithScrollView:(UIScrollView *)scrollView
{
    NSUInteger index = round(scrollView.contentOffset.x/scrollView.size.width);
    BOOL needScrollToFirst = false;
    BOOL needScrollToLast = false;
    if (self.canScroll) {
        if (index == 0) {
            index = self.imageViews.count -3;
            needScrollToLast = true;
        }else if (index >= self.imageViews.count - 1) {
            index = 0;
            needScrollToFirst = true;
        }else {
            index = index - 1;
        }
    }

    self.index = index;
    !needScrollToFirst?:[self scrollToFirst];
    !needScrollToLast?:[self scrollToLast];
}

- (void)killTimer
{
    if (_timer.isValid) {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)dealloc
{
    [self killTimer];
}
@end
